package com.skyline.base.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.skyline.base.dao.FootprintDao;
import com.skyline.base.mapper.FootprintMapper;
import com.skyline.base.model.Footprint;
import com.skyline.base.type.ResourceType;
import com.skyline.common.bean.Page;
import com.skyline.common.cache.annotation.Cache;
import com.skyline.common.cache.annotation.CacheCategoryType;
import com.skyline.common.cache.annotation.CacheDelete;
import com.skyline.common.cache.annotation.CombFk;
import com.skyline.common.cache.annotation.Param;

@Repository("footprintDao")
public class FootprintDaoImpl extends BaseDaoImpl implements FootprintDao {
	/**
	 * select * from footprint where viewerId=? and resourceId=? and
	 * resourceType=? limit 1
	 */
	@Value("${FootprintDao.queryFootprint}")
	private String queryFootprintSql;

	/**
	 * select * from footprint where resourceId=? and resourceType=?
	 */
	@Value("${FootprintDao.queryFootprintsOfResource}")
	private String queryFootprintsOfResourceSql;

	/**
	 * select * from footprint where resourceId=? and resourceType=? limit ?
	 */
	@Value("${FootprintDao.querySomeFootprintsOfResource}")
	private String querySomeFootprintsOfResourceSql;

	/**
	 * insert into
	 * footprint(viewerId,viewerNickname,viewerPortrait,viewerEmail,resourceId
	 * ,resourceType,lastVisitTime) values (?,?,?,?,?,?,current_time)
	 */
	@Value("${FootprintDao.insertFootprint}")
	private String insertFootprintSql;

	/**
	 * update footprint
	 * viewerNickname=?,viewerPortrait=?,viewerEmail=?,lastVisitTime
	 * =current_time where viewerId=? and resourceId=? and resourceType=?
	 */
	@Value("${FootprintDao.updateFootprint}")
	private String updateFootprintSql;

	@Cache(keyPattern = "fp-vid:vid-rid:rid-rtype:rtype", type = CacheCategoryType.FOORPRINT)
	public Footprint queryFootprint(@Param("vid") Long viewerId, @Param("rid") Long resourceId,
			@Param("rtype") ResourceType resourceType) {
		List<Footprint> footprints = jdbcTemplate.query(queryFootprintSql, FootprintMapper.getMapper(), viewerId,
				resourceId, resourceType.toString());
		if (CollectionUtils.isEmpty(footprints)) {
			return null;
		} else {
			return footprints.get(0);
		}
	}

	@Cache(keyPattern = "fp-l-rid:rid-rtype:rtype", type = CacheCategoryType.FOORPRINT, pagination = true)
	public List<Footprint> queryFootprintsOfResource(@CombFk(order = 2) @Param("rid") Long resourceId,
			@CombFk(order = 1) @Param("rtype") ResourceType resourceType, Page page) {
		return getPaginationResult(queryFootprintsOfResourceSql, page, FootprintMapper.getMapper(), resourceId,
				resourceType);
	}

	@Cache(keyPattern = "fp-l-s-rid:rid-rtype:rtype-n:num", type = CacheCategoryType.FOORPRINT)
	public List<Footprint> querySomeFootprintsOfResource(@CombFk(order = 2) @Param("rid") Long resourceId,
			@CombFk(order = 1) @Param("rtype") ResourceType resourceType, @Param("num") Integer size) {
		return jdbcTemplate.query(querySomeFootprintsOfResourceSql, FootprintMapper.getMapper(), resourceId,
				resourceType.toString(), size);
	}

	@CacheDelete(type = CacheCategoryType.FOORPRINT)
	public void insertFootprint(Long viewerId, String viewerNickname, String viewerPortrait, String viewerEmail,
			@CombFk(order = 2) Long resourceId, @CombFk(order = 1) ResourceType resourceType) {
		jdbcTemplate.update(insertFootprintSql, viewerId, viewerNickname, viewerPortrait, viewerEmail, resourceId,
				resourceType.toString());
	}

	@CacheDelete(type = CacheCategoryType.FOORPRINT)
	public void updateFootprint(Long viewerId, String viewerNickname, String viewerPortrait, String viewerEmail,
			@CombFk(order = 2) Long resourceId, @CombFk(order = 1) ResourceType resourceType) {
		jdbcTemplate.update(updateFootprintSql, viewerNickname, viewerPortrait, viewerEmail, viewerId, resourceId,
				resourceType.toString());
	}

}
